﻿using System.Globalization;
using System.Windows.Data;
using DimensionsHelper.Common.Contracts.Excel;

namespace DimensionsHelper.Client.Converters;

public class TextDecorationConverter : IValueConverter, IMultiValueConverter
{
    private static readonly TextDecorationCollection DoubleLine = new()
    {
        new TextDecoration
        {
            Location = TextDecorationLocation.Underline
        },
        new TextDecoration
        {
            Location = TextDecorationLocation.Underline,
            PenOffset = 1.5
        }
    };

    private static readonly TextDecorationCollection SingleAccountingLine = new()
    {
        new TextDecoration
        {
            Location = TextDecorationLocation.Underline,
            PenOffset = 1
        }
    };

    private static readonly TextDecorationCollection DoubleAccountingLine = new()
    {
        new TextDecoration
        {
            Location = TextDecorationLocation.Underline,
            PenOffset = 1
        },
        new TextDecoration
        {
            Location = TextDecorationLocation.Underline,
            PenOffset = 2.5
        }
    };


    private static readonly TextDecorationCollection EmptyLine = new();

    public static TextDecorationConverter Instance { get; } = new();


    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        bool underlined = false;
        UnderLineType underLineType = UnderLineType.None;

        foreach (object item in values)
        {
            switch (item)
            {
                case bool addUnderline:
                    underlined = addUnderline;
                    break;

                case UnderLineType ut:
                    underLineType = ut;
                    break;
            }
        }

        if (underlined && underLineType != UnderLineType.None)
        {
            return underLineType switch
            {
                UnderLineType.Single => TextDecorations.Underline,
                UnderLineType.Double => DoubleLine,
                UnderLineType.SingleAccounting => SingleAccountingLine,
                UnderLineType.DoubleAccounting => DoubleAccountingLine,
                _ => EmptyLine
            };
        }

        return EmptyLine;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        return [];
    }

    public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        if (value is not UnderLineType underLineType || underLineType == UnderLineType.None)
        {
            return EmptyLine;
        }

        return underLineType switch
        {
            UnderLineType.Single => TextDecorations.Underline,
            UnderLineType.Double => DoubleLine,
            UnderLineType.SingleAccounting => SingleAccountingLine,
            UnderLineType.DoubleAccounting => DoubleAccountingLine,
            _ => EmptyLine
        };
    }

    public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        return Binding.DoNothing;
    }
}